﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using PSE.Framework.UI.Resources;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public class SinacorReportFieldOrderWindow : SinacorWindow
    {
        #region Fields

        private SinacorListBox _properties = null;
        private SinacorButton _btnUp = null;
        private SinacorButton _btnDown = null;
        private string _reportTitleResourceKey;

        #endregion

        #region Properties

        public ObservableCollection<SinacorReportFormatObjects> OrderFormatObjects { get; set; }

        private Boolean _canceled=true;
        public Boolean Canceled
        {
            get
            {
                return _canceled;
            }
        }

        #endregion

        #region Constructor

        private SinacorReportFieldOrderWindow()
        {
        }

        public SinacorReportFieldOrderWindow(List<SinacorReportFormatObjects> formatObjects, string reportTitleResourceKey)
        {
            base.AutoSaveLayoutEnabled = false;

            this._reportTitleResourceKey = reportTitleResourceKey;

            this.SetResourceReference(StyleProperty, "ControlReportFieldOrderWindow");
            this.SetResourceReference(Window.TitleProperty, reportTitleResourceKey);

            this.CommandBindings.Add(new CommandBinding(SinacorWindowCommands.Close, btnClose_Click));
            this.CommandBindings.Add(new CommandBinding(SinacorReportCommands.Ok, btnOk_Click));
            this.CommandBindings.Add(new CommandBinding(SinacorReportCommands.Cancel, btnCancel_Click));

            this.CommandBindings.Add(new CommandBinding(SinacorReportCommands.Up, btnUp_Click));
            this.CommandBindings.Add(new CommandBinding(SinacorReportCommands.Down, btnDown_Click));

            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            this.OrderFormatObjects = new ObservableCollection<SinacorReportFormatObjects>();
            foreach (SinacorReportFormatObjects item in formatObjects)
            {
                if (item.IsVisible)
                    this.OrderFormatObjects.Add(item);
            }

            this.Initialized += new EventHandler(SinacorReportParameterWindow_Initialized);
        }

        void SinacorReportParameterWindow_Initialized(object sender, EventArgs e)
        {
            SinacorWindow ownerWindow = this.Owner as SinacorWindow;
            if (ownerWindow == null)
                throw new Exception("The Owner of the Window must be informed.");

            this.ModuleId = ownerWindow.ModuleId;
            this.ModulePlus = ownerWindow.ModulePlus;
            this.SetMenuHeader(ownerWindow.MenuHeader, this._reportTitleResourceKey);
        }
        #endregion

        #region Buttons

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this._canceled = true;
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this._canceled = false;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this._canceled = true;
            this.Close();
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            int index = this._properties.SelectedIndex;
            SinacorReportFormatObjects item = (SinacorReportFormatObjects)this._properties.SelectedItem;

            this.OrderFormatObjects.RemoveAt(index);
            this.OrderFormatObjects.Insert(index - 1, item);

            this._properties.SelectedIndex = index - 1;
        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {
            int index = this._properties.SelectedIndex;
            SinacorReportFormatObjects item = (SinacorReportFormatObjects)this._properties.SelectedItem;

            this.OrderFormatObjects.RemoveAt(index);
            this.OrderFormatObjects.Insert(index + 1, item);

            this._properties.SelectedIndex = index + 1;
        }

        #endregion

        #region OnApplyTemplate

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            DockPanel header = this.GetTemplateChild("LayoutRoot") as DockPanel;
            header.MouseLeftButtonDown += new MouseButtonEventHandler(header_MouseLeftButtonDown);

            this._properties = this.GetTemplateChild("LstProperties") as SinacorListBox;
            this._btnUp = this.GetTemplateChild("BtnUp") as SinacorButton;
            this._btnDown = this.GetTemplateChild("BtnDown") as SinacorButton;

            this._properties.ItemsSource = this.OrderFormatObjects;
            this._properties.DisplayMemberPath = "TitleColumn";

            this._properties.SelectionChanged += new SelectionChangedEventHandler(_properties_SelectionChanged);
            this._properties_SelectionChanged(this._properties, null);
        }

        #endregion

        #region header_MouseLeftButtonDown

        void header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        #endregion

        #region _properties_SelectionChanged

        void _properties_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this._btnUp.IsEnabled = this._properties.SelectedIndex > 0;
            this._btnDown.IsEnabled = (this._properties.SelectedIndex + 1) < this._properties.Items.Count;
        }

        #endregion
    }
}
